package pers.liu.yibandemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.liu.yibandemo.entity.SchoolDepartment;
import pers.liu.yibandemo.service.SchoolDepartmentService;

import java.util.List;

@RestController
@RequestMapping("/departments")
public class SchoolDepartmentController {

    @Autowired
    private SchoolDepartmentService departmentService;

    @PostMapping("/add")
    public ResponseEntity<String> addDepartment(@RequestBody SchoolDepartment department) {
        departmentService.addDepartment(department);
        return ResponseEntity.ok("Department added successfully.");
    }

    @GetMapping("/{id}")
    public ResponseEntity<SchoolDepartment> getDepartmentById(@PathVariable Long id) {
        SchoolDepartment department = departmentService.getDepartmentById(id);
        if (department != null) {
            return ResponseEntity.ok(department);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/all")
    public ResponseEntity<List<SchoolDepartment>> getAllDepartments() {
        List<SchoolDepartment> departments = departmentService.getAllDepartments();
        return ResponseEntity.ok(departments);
    }

    @PutMapping("/update")
    public ResponseEntity<String> updateDepartment(@RequestBody SchoolDepartment department) {
        departmentService.updateDepartment(department);
        return ResponseEntity.ok("Department updated successfully.");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteDepartment(@PathVariable Long id) {
        departmentService.deleteDepartment(id);
        return ResponseEntity.ok("Department deleted successfully.");
    }
    @GetMapping("/page")
    public ResponseEntity<IPage<SchoolDepartment>> getDepartmentsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize) {
        IPage<SchoolDepartment> departments = departmentService.getDepartmentsByPage(pageNum, pageSize);
        return ResponseEntity.ok(departments);
    }
}
